package label.action;

import blogData.entity.BlogDataVO;
import blogData.service.BlogDataService;
import com.opensymphony.xwork2.ActionSupport;
import generic.page.Pages;
import label.entity.LabelVO;
import label.service.LabelService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import util.ObtainOther;

public class LabelAction extends ActionSupport {

    private final static Log LOGER = LogFactory.getLog(LabelAction.class);

    private LabelService labelService;

    private LabelVO labelVO;

    private Pages pages;

    private String message;

    private Integer blogDataId ;

    private Integer labelId;

    private BlogDataService blogDataService;

    public BlogDataService getBlogDataService() {
        return blogDataService;
    }

    public void setBlogDataService(BlogDataService blogDataService) {
        this.blogDataService = blogDataService;
    }

    public Integer getLabelId() {
        return labelId;
    }

    public void setLabelId(Integer labelId) {
        this.labelId = labelId;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public LabelService getLabelService() {
        return labelService;
    }

    public void setLabelService(LabelService labelService) {
        this.labelService = labelService;
    }

    public Pages getPages() {
        return pages;
    }

    public void setPages(Pages pages) {
        this.pages = pages;
    }

    public LabelVO getLabelVO() {
        return labelVO;
    }

    public void setLabelVO(LabelVO labelVO) {
        this.labelVO = labelVO;
    }


    /**
     * 删除标签
     *
     * @return
     */
    public String deleteLabel() {
        try {
            labelService.deleteLabelById(labelId);
            message= ObtainOther.obtainMessage("deleteLabelSuccess");
            return SUCCESS;
        } catch (Exception e) {
            message= ObtainOther.obtainMessage("deleteLabelError");
            LOGER.error(message + ":/n" + e);
            return ERROR;
        }
    }

    /**
     * 更新标签
     *
     * @return
     */
    public String updateLabel() {
        try {
            labelVO = labelService.updateLabel(labelVO);
            message= ObtainOther.obtainMessage("updateLabelSuccess");
            return SUCCESS;
        } catch (Exception e) {
            message= ObtainOther.obtainMessage("updateLabelError");
            LOGER.error(message + ":/n" + e);
            return ERROR;
        }
    }

    /**
     * 分页显示标签
     * @return
     */
    public String listLabel() {
        try {
            blogDataId=ObtainOther.obtainId();

            pages = labelService.queryForPage(pages,blogDataId);
            return SUCCESS;
        } catch (Exception e) {
            message= ObtainOther.obtainMessage("listLabelError");
            LOGER.error(message + ":/n" + e);
            e.printStackTrace();
            return ERROR;
        }
    }

    /**
     * 读取标签
     * @return
     */
    public String loadLabel() {
        try {
            labelVO = labelService.findLabelVOById(labelId);
            return SUCCESS;
        } catch (Exception e) {
            message= ObtainOther.obtainMessage("loadLabelError");
            LOGER.error(message + ":/n" + e);
            return ERROR;
        }
    }

    /**
     * 添加标签
     * @return
     */
    public String addLabel() {
        try {
            blogDataId=ObtainOther.obtainId();
            BlogDataVO blogDataVO=blogDataService.findBlogDataVOById(blogDataId);
            labelVO = labelService.addLabelByBlogData(labelVO,blogDataVO);
            return SUCCESS;
        } catch (Exception e) {

            message= ObtainOther.obtainMessage("loadLabelError");
            LOGER.error(message + ":/n" + e);
            return ERROR;
        }
    }
}
